package cn.swpu.blog.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo implements Serializable, UserDetails {
    /**
     * openid
     */
    private String openid;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 性别：默认男
     */
    private String gender;

    /**
     * 用户头像
     */
    private String figureurlQq2;

    /**
     * 权限
     * 1：用户
     * 0：管理员
     */
    private Integer role;

    private static final long serialVersionUID = 1L;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        Collection<SimpleGrantedAuthority> collection = new HashSet<>();
        collection.add(new SimpleGrantedAuthority("ROLE_USER"));

        if (role == 0) {
            collection.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }

        return collection;
    }

    @Override
    public String getPassword() {
        return null;
    }

    /**
     * @return 返回用户的openid
     */
    @Override
    public String getUsername() {
        return openid;
    }

    /**
     * 账户是否未锁定，默认未锁定
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未锁定，默认未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否未过期，默认未过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否激活，默认激活
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}